package org.jflame.commons.config;

import java.io.IOException;
import java.util.List;
import java.util.Properties;

import org.apache.commons.lang3.exception.ExceptionUtils;

/**
 * 静态化配置读取类,使得可以使用静态方方法便捷访问配置资源
 * 
 * @see ConfigReader
 * @author charles.zhang
 */
public abstract class ConfigHolder {

    private static ConfigReader config;

    /**
     * 设置配置该读取类
     * 
     * @param _config
     */
    public static void setConfig(ConfigReader _config) {
        synchronized (ConfigHolder.class) {
            ConfigHolder.config = _config;
        }
    }

    public static ConfigReader getConfig() {
        return ConfigHolder.config;
    }

    public synchronized static void loadProperties(String... propertiesFils) throws IOException {
        ConfigHolder.setConfig(new PropertiesConfig(propertiesFils));
    }

    public synchronized static void loadProperties(Properties props) {
        ConfigHolder.setConfig(new PropertiesConfig(props));
    }

    /**
     * 加载默认配置文件,classpath下的system.properties
     */
    public static void loadDefaultProperties() {
        try {
            loadProperties("classpath:system.properties");
        } catch (IOException e) {
            ExceptionUtils.rethrow(e);
        }
    }

    public static String getString(final String configKey) {
        return config.getString(configKey);
    }

    public static String getString(final String configKey, final String defaultValue) {
        return config.getString(configKey, defaultValue);
    }

    public static Integer getInt(final String configKey) {
        return config.getInt(configKey);
    }

    public static Integer getInt(final String configKey, final int defaultValue) {
        return config.getInt(configKey, defaultValue);
    }

    public static Boolean getBoolean(final String configKey) {
        return config.getBoolean(configKey);
    }

    public static Boolean getBoolean(final String configKey, final boolean defaultValue) {
        return config.getBoolean(configKey, defaultValue);
    }

    public static Long getLong(final String paramKey) {
        return config.getLong(paramKey);
    }

    public static Double getDouble(final String configKey) {
        return config.getDouble(configKey);
    }

    public static Double getDouble(final String configKey, final double defaultValue) {
        return config.getDouble(configKey, defaultValue);
    }

    public static String[] getStringArray(final String configKey) {
        return config.getStringArray(configKey);
    }

    public static List<String> getStrings(final String configKey) {
        return config.getStringList(configKey);
    }

    public static int[] getIntArray(final String configKey) {
        return config.getIntArray(configKey);
    }

    public static List<Integer> getIntegers(final String configKey) {
        return config.getIntegerList(configKey);
    }

}
